﻿using System;
using System.Collections;
using System.Collections.Generic;
using UniRx.Async;
using UnityEngine;

public class Init : MonoBehaviour
{
    public int TargetFrameRate;
    //默认不选，从服务器下载
    public bool isABNotFromServer;
    public bool isABUsedYunSever;
    public string selfResourceServerIpAndPort;
    public string versionGameCode;
    public string GameShowVersion;
    public bool isShowSelfLog;
    public bool isUseAssetBundle;

    // Start is called before the first frame update
    private async void Start()
    {
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        Application.targetFrameRate = TargetFrameRate;
        StaticData.IsABNotFromServer = isABNotFromServer;
        StaticData.isABUsedYunSever = isABUsedYunSever;
        StaticData.SelfResourceServerIpAndPort = selfResourceServerIpAndPort;
        StaticData.localVersionGameCode = versionGameCode;
        StaticData.GameShowVersion = GameShowVersion;
        StaticData.isShowSelfLog = isShowSelfLog;
        try 
        {
            StaticData.isUseAssetBundle = isUseAssetBundle;
            DontDestroyOnLoad(gameObject);
            gameObject.AddComponentIfNull<GlobalConfigComponent>();
            gameObject.AddComponentIfNull<ResourcesComponent>();
            BundleDownloaderComponent bundleDownloaderComponent = gameObject.AddComponentIfNull<BundleDownloaderComponent>();
            //Game.EventSystem.Run(EventIdType.LoadingBegin);//打开进度条，最外层的
            var IsGameVersionCodeEqual = await BundleHelper.IsGameVersionCodeEqual();
            if (!IsGameVersionCodeEqual)
            {
                return;
            }
            // 下载ab包
            await BundleHelper.DownloadBundle();
            GameObject.Destroy(bundleDownloaderComponent);
            //AB初始化
            ABManager.Init();
            //数据表格解析
            ParseExcelData parseExcelData = new ParseExcelData();
            StaticData.configExcel = parseExcelData.Init();

            ResourcesHelper.CreateUI("UIMap");
            await UniTask.DelayFrame(1);
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
